package com.project.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.common.api.Result;
import com.project.server.dto.ConsultationCreateDTO;
import com.project.server.dto.MessageDTO;
import com.project.server.entity.Consultation;
import com.project.server.entity.ConsultationMessage;
import com.project.server.service.ConsultationMessageService;
import com.project.server.service.ConsultationService;
import com.project.server.vo.ConsultationDetailVO;
import com.project.server.vo.ConsultationMessageVO;
import com.project.server.vo.ConsultationStats;
import com.project.server.vo.ConsultationVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/consultations")
class ConsultationController extends BaseController {

    @Autowired
    private ConsultationService consultationService;


    @Autowired

    private  ConsultationMessageService consultationMessageService;

    @PostMapping("/create")
    public Result<Long> createConsultation(@RequestBody ConsultationCreateDTO dto,HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        Long consultationId = consultationService.createConsultation(dto.getDoctorId(),userId);
        return Result.success(consultationId);
    }

    @GetMapping("/detail/{id}")
    public Result<ConsultationDetailVO> getConsultationDetail(@PathVariable Long id) {
        ConsultationDetailVO detail = consultationService.getConsultationDetail(id);
        return Result.success(detail);
    }

    @GetMapping("/{id}/messages")
    public Result<List<ConsultationMessageVO>> getMessages(@PathVariable Long id) {
        List<ConsultationMessageVO> messages = consultationMessageService.getConsultationMessages(id);
        return Result.success(messages);
    }

    @PostMapping("/{id}/message")
    public Result<Long> sendMessage(@PathVariable Long id, @RequestBody MessageDTO dto,HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        Long messageId = consultationMessageService.sendMessage(id, dto,userId);
        return Result.success(messageId);
    }

    @PostMapping("/message")
    public Result<Void> saveMessage(@RequestBody ConsultationMessage message,HttpServletRequest request) {
        message.setCreateTime(LocalDateTime.now());
        // 当前登录人是医生id
        Long currentUserId = getCurrentUserId(request);
        message.setFromUserId(currentUserId);
        consultationMessageService.save(message);
        return Result.success();
    }

    @GetMapping
    public Result<Page<Consultation>> getList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status,
            HttpServletRequest request) {
        return Result.success(consultationService.getConsultations(
            getCurrentUserId(request), current, size, status));
    }


    @PutMapping("/{id}/accept")
    public Result<Void> accept(@PathVariable Long id) {
        consultationService.acceptConsultation(id);
        return Result.success();
    }

    @PutMapping("/{id}/reject")
    public Result<Void> reject(@PathVariable Long id) {
        consultationService.rejectConsultation(id);
        return Result.success();
    }

    @PutMapping("/{id}/end")
    public Result<Void> end(
            @PathVariable Long id,
            @RequestParam String remark) {
        consultationService.endConsultation(id, remark);
        return Result.success();
    }


    @GetMapping("/stats")
    public Result<ConsultationStats> getStats(HttpServletRequest request) {
        return Result.success(consultationService.getConsultationStats(getCurrentUserId(request)));
    }

    // 获取用户问诊记录
    @GetMapping("/user")
    public Result<Page<ConsultationVO>> getUserConsultations(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status,
            HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        Page<ConsultationVO> page = consultationService.getUserConsultations(userId, status, current, size);
        return Result.success(page);
    }
}
